package com.example.demo1.controller;


import com.example.demo1.common.Result;
import com.example.demo1.controller.request.UserRequest;
import com.example.demo1.entity.User;
import com.example.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@ResponseBody
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserRequest user) throws Exception{
        String username=user.getUsername();
        String password=user.getPassword();
        System.out.println(username+"-------"+password);
        if(userService.isUserExist(username,password)){
            return Result.success("success");
        }else{
            return Result.fail("failure");
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) throws Exception{
        try{
            userService.addUser(user);
            return Result.success("success");
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }



    @PostMapping("/logout")
    public Result logout() throws Exception {
        return Result.success("success");
    }


}
